package com.www.applyword.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.bean.DynaminBean;
import com.www.applyword.bean.LoginBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.ImageUtil;
import com.www.applyword.utils.SPUtil;
import com.www.applyword.view.MyGridView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author : huang
 * date   : 2019/10/21 0021 15:06
 * version: 1.0
 * @author Administrator
 */
public class DynamicAdapter extends CommonRecyclerAdapter<DynaminBean.ListBean> {
    private Context context;
    private List<Boolean> alreadyLikeList = new ArrayList<>();
    private ImageView view;


    public DynamicAdapter(Context context) {
        super(context, R.layout.item_list_dynamic);
        this.context = context;
    }


    @Override
    public void onUpdate(BaseAdapterHelper helper, DynaminBean.ListBean item, int position) {
        ImageUtil.setImage(helper.getView(R.id.civ_personal_head_image), (String) item.getForum_UserImg());
        helper.setText(R.id.tv_name, (String) item.getForum_UserName());
        helper.setText(R.id.tv_time, (String) item.getForum_CreateTime());
        helper.setText(R.id.tv_dynamic_detail, (String) item.getForum_Content());
        helper.setText(R.id.tv_msg, item.getForum_CommentNum() + "");
        helper.setText(R.id.tv_like,  item.getForum_PraiseNum() + "");
        MyGridView view = helper.getView(R.id.rv_dynamic_photos);
        ImageView imageView = helper.getView(R.id.iv_like);
        TextView tv_like = helper.getView(R.id.tv_like);

        // 0 没有 点赞  非1 是点赞(false)
        alreadyLikeList.add(position,!item.getForum_IsPraise().equals("0"));
        if (alreadyLikeList.get(position)){
            imageView.setImageResource(R.mipmap.like1);
            // alreadyLikeList.add(itemList.getIs_like().equals("1")); //0 未点赞
        }else {
            imageView.setImageResource(R.mipmap.like);
            //  alreadyLikeList.add(itemList.getIs_like().equals("0")); //已点赞
        }

        helper.getView(R.id.ll_like).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, Object> params = new HashMap<>();
                params.put("UserID", SPUtil.getString(Constants.USERID));
                params.put("ForumID",item.getForum_ID());
                //点赞
                if (alreadyLikeList.get(position)){
                    params.put("Type", 1 + ""); //取消点赞
                }else {
                    params.put("Type",0 + ""); //点赞
                }

                HttpClient.get(context, Constants.GIVETHETHUMBSUP, params,new CallBack<LoginBean>() {
                    @Override
                    public void onSuccess(LoginBean result) {
                        if (alreadyLikeList.get(position)){
                            imageView.setImageResource(R.mipmap.like);
                            tv_like.setText(Integer.valueOf(tv_like.getText().toString()) - 1 + "");
                            alreadyLikeList.set(position,false);
                        }else{
                            imageView.setImageResource(R.mipmap.like1);
                            tv_like.setText(Integer.valueOf(tv_like.getText().toString()) + 1 + "");
                            alreadyLikeList.set(position,true);
                        }
                    }
                });
            }
        });

        List<String> list = new ArrayList<>();
        FansImagesAdpter fansImagesAdpter = new FansImagesAdpter(context, list);
        view.setAdapter(fansImagesAdpter);
        if (!TextUtils.isEmpty(item.getForum_Img()) && item.getForum_Img().contains(",")) {
            list.addAll(Arrays.asList(item.getForum_Img().split(",")));
           // List<String> list = Arrays.asList(item.getForum_Img().split(","));
            fansImagesAdpter.notifyDataSetChanged();
           /* if (list.size() > 0) {
                FansImagesAdpter fansImagesAdpter = new FansImagesAdpter(context, list);
                view.setAdapter(fansImagesAdpter);
            }*/
        }else {
            if (TextUtils.isEmpty(item.getForum_Img())){
               list.clear();
                fansImagesAdpter.notifyDataSetChanged();
            }else {
                list.addAll(Arrays.asList(item.getForum_Img()));
                fansImagesAdpter.notifyDataSetChanged();
              //  FansImagesAdpter fansImagesAdpter = new FansImagesAdpter(context, list);
              //  view.setAdapter(fansImagesAdpter);
            }
        }
    }
}

